# 将csv表全部合并为一个
import os
import pandas as pd
import os
from datetime import datetime
from dateutil.relativedelta import relativedelta


def get_root_dir():
    root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
    return root_dir

def calculate_previous_day():
    current_date = datetime.now()
    previous_date = current_date - relativedelta(days=1)
    return previous_date.strftime("%Y_%m_%d")


def merge_csv_files(input_folder, output_file):
    # 获取文件夹中所有 CSV 文件的路径
    csv_files = [f for f in os.listdir(input_folder) if f.endswith(".csv")]

    # 创建一个空的 DataFrame 用于存储合并后的数据
    combined_df = pd.DataFrame()

    # 遍历所有 CSV 文件并合并
    for file in csv_files:
        file_path = os.path.join(input_folder, file)
        df = pd.read_csv(file_path)
        combined_df = pd.concat([combined_df, df], ignore_index=True)

    combined_df.rename(
        columns={
            "上座率": "上座率(%)",
            "票房占比": "票房占比(%)",
            "排片占比": "排片占比(%)",
            "当日票房": "当日票房(万)",
            "当日总出票": "当日总出票(万)",
            "当日总场次": "当日总场次(万)",
        },
        inplace=True,
    )

    cols_to_process = ["上座率(%)", "票房占比(%)", "排片占比(%)"]
    for col in cols_to_process:
        combined_df[col] = combined_df[col].astype(str).str.rstrip("%")

    cols_to_process = ["当日总出票(万)", "当日总场次(万)"]
    for col in cols_to_process:
        combined_df[col] = combined_df[col].astype(str).str.rstrip("万")

    combined_df["已上映天数"] = (
        combined_df["已上映天数"].str.extract(r"上映(\d+)天").astype(float)
    )

    combined_df = combined_df[~(combined_df["上座率(%)"] == "--")]
    combined_df = combined_df[~(combined_df["已上映天数"] == "")]
    combined_df = combined_df[~(combined_df["已上映天数"].isna())]

    combined_df = combined_df.astype(str).replace("<", "", regex=True)

    # 将合并后的数据写入新的 CSV 文件
    combined_df.to_csv(output_file, index=False)
    print("success")


# 示例用法
input_folder_in = f"{get_root_dir()}/爬取到的数据/最新数据/csv缓存"  # 替换为你的 CSV 文件夹路径
output_file = f"{get_root_dir()}/爬取到的数据/最新数据/数据处理/all_movie_{calculate_previous_day()}.csv"  # 替换为你想要保存的输出文件名
merge_csv_files(input_folder_in, output_file)
